<?php

/**
 * transaction actions.
 *
 * @package    tmdb
 * @subpackage transaction
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class transactionActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->transaction_list = Doctrine::getTable('Transaction')
      ->createQuery('a')
 			->where('a.client_id = ?',$request->getParameter('client_id'))
      ->execute();
		$this->client_id = $request->getParameter('client_id');
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->transaction = Doctrine::getTable('Transaction')->find(array($request->getParameter('id')));
		$this->client_id = $this->transaction->getclient_id();
    $this->forward404Unless($this->transaction);
  }

  public function executeNew(sfWebRequest $request)
  {
		$transaction = new Transaction();
		$transaction->setclient_id($request->getParameter('client_id'));
		$this->client_id = $request->getParameter('client_id');
    $this->form = new TransactionForm($transaction);
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));
		$transaction = new Transaction();
		$transaction->setclient_id($request->getParameter('client_id'));
    $this->form = new TransactionForm();

    $this->processForm($request, $this->form);
		$this->client_id = $transaction->getclient_id();
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($transaction = Doctrine::getTable('Transaction')->find(array($request->getParameter('id'))), sprintf('Object transaction does not exist (%s).', array($request->getParameter('id'))));
		$this->client_id = $transaction->getclient_id();
    $this->form = new TransactionForm($transaction);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($transaction = Doctrine::getTable('Transaction')->find(array($request->getParameter('id'))), sprintf('Object transaction does not exist (%s).', array($request->getParameter('id'))));
    $this->form = new TransactionForm($transaction);

    $this->processForm($request, $this->form);
		$this->client_id = $transaction->getclient_id();
    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($transaction = Doctrine::getTable('Transaction')->find(array($request->getParameter('id'))), sprintf('Object transaction does not exist (%s).', array($request->getParameter('id'))));
		$this->client_id = $transaction->getclient_id();
    $transaction->delete();

    $this->redirect('transaction/index?client_id='.$this->client_id);
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()));
    if ($form->isValid())
    {
      $transaction = $form->save();
		$this->client_id = $transaction->getclient_id();
      $this->redirect('transaction/edit?id='.$transaction['id']);
    }
  }
}
